package com.zlq.cn.controller.sign.lovers;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlq.cn.api.SignApiResult;
import com.zlq.cn.bootstrap.SignApiBootstrap;
import com.zlq.cn.entity.LoversTime;
import com.zlq.cn.entity.User;
import com.zlq.cn.service.lovers.LoversTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * <p>
 * 小情侣-时光 前端控制器
 * </p>
 *
 * @author zhoucl
 * @since 2022-05-08
 */
@Api(tags = "小情侣-时光")
@RestController
@RequestMapping("/lovers/time")
public class LoversTimeController {

    @Resource
    private LoversTimeService loversTimeService;

    @ApiOperation("新增")
    @PostMapping("/add")
    public SignApiResult<String> add(@Valid @RequestBody LoversTime loversTime) {
        User user = SignApiBootstrap.requestUser();
        loversTime.setUserId(user.getId())
                .setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now());
        loversTimeService.save(loversTime);
        return SignApiResult.resultSuccess("新增成功");
    }

    @ApiOperation(value = "编辑", notes = "跟新增不同的是需要传递id")
    @PostMapping("/edit")
    public SignApiResult<String> edit(@Valid @RequestBody LoversTime loversTime) {
        Assert.notNull(loversTime.getLoversTimeId(), "时光id不能为空");
        loversTime.setUpdateTime(LocalDateTime.now());
        loversTimeService.updateById(loversTime);
        return SignApiResult.resultSuccess("新增成功");
    }

    @ApiOperation(value = "删除", notes = "只需要传递id即可")
    @PostMapping("/remove")
    public SignApiResult<String> remove(@Valid @RequestBody LoversTime loversTime) {
        loversTimeService.removeById(loversTime.getLoversTimeId());
        return SignApiResult.resultSuccess("删除成功");
    }

    @ApiOperation(value = "获取时光列表")
    @PostMapping("/list")
    public SignApiResult<Page<LoversTime>> list(@RequestBody Page<LoversTime> page) {
        return SignApiResult.resultSuccess(loversTimeService.pageLoversTime(page));
    }
}
